<?php

class topup_ewallet_form extends CFormModel {

    public $serial_pin;
    public $kode_unik;
    public $jumlah_ditransfer;

    /**
     * Declares the validation rules.
     */
    public function rules() {
        return array(
            array('serial_pin_part1', 'validasi_serial_topup'),
            array('pin_topup', 'validasi_serial_topup'),
            array('serial_pin', 'validasi_pin'),
        );
    }

    public function validasi_serial_topup($attribute, $params) {

        $post_serial = $_POST['serial_pin_part1'];
        $post_pin = $_POST['pin_topup'];
        $serial_model = Yii::app()->db->createCommand()
                ->from('sys_serial_topup')
                ->where('serial_topup_id=:id', array(':id' => $post_serial))
                ->queryRow();

        $serial = $serial_model['serial_topup_id'];
        $pin = $serial_model['serial_topup_pin'];
        $is_used = $serial_model['serial_topup_is_used'];

        if( empty($post_serial) || empty($post_pin) ) {
            $this->addError('serial', 'Serial & Pin Topup Tidak boleh kosong.');
        } elseif ( $post_serial != $serial && $post_pin != $pin ) {
           $this->addError('serial', 'Serial & Pin Topup Anda Salah.');
        } elseif( $is_used == '1' ) {
            $this->addError('serial', 'Serial Anda sudah dipakai.');
        }
    }

    public function validasi_pin($attribute, $params) {

        $pin = $_POST['serial_pin'];
        if (!function_lib::pin_validation(Yii::app()->user->getId(), $pin)) {
//            $messages = Yii::t('yii', "Pin Salah");
//            $this->addError($attribute, $messages);
            $this->addError('serial_pin', 'Pin Anda Salah.');
        }
    }

}

?>
